<?php

namespace app\api\controller;

use app\api\model\ConfigSystem;
use think\Db;
use Vectorface\Whip\Whip;

class Index extends Common
{
    public function shangjia()
    {
        $config = ConfigSystem::where(['category' => 'push'])->column('title, content');

        return json([['shang' => $config['sshang'],'pshang' => $config['pshangjia']]]);
    }
    
    /*public function rr()
    {
        $males = Db::name('user')->where(['id' => ['gt', 6208], 'role' => 0, 'yqr' => ['neq', '']])->column('id, yqr');
        
        $female = Db::name('user')->where(['yqm' => ['in', $males]])->column('yqm, id');
        
        $chat = [];
        foreach ($males as $key => $item) {
            if (isset($female[$item])) {
                $chat[] = [
                    'userid' => $female[$item],
                    'touserid' => $key,
                    'content' => '我在这儿呢',
                    'sound_tip' => 1,
                    'finish' => 1,
                    'sms_tip' => 1,
                ];
            }
        }
        
        Db::name('privatechat')->insertAll($chat);
    }*/
    
    public function delGreet()
    {
        /*$privatechat = Db::name('privatechat_default')->where(['types' => 2, 'channel' => 2])->column('content');
        
        $data = Db::name('privatechat')->field('content, count(*) count')->where(['content' => ['in', $privatechat]])->group('content')->select();
        
        $contain = [];
        foreach ($data as $item) {
            if ($item['count'] > 2) {
                $contain[] = $item['content'];
            }
        }
        
        if ($contain) {
            $count = Db::name('privatechat')->where(['content' => ['in', $contain]])->delete();
            halt($count);
        }*/
        exit;
        $sql = "delete FROM privatechat where type = 0 and content <> '我在这儿呢' and (userid,content)
            IN
            (SELECT userid,content FROM (SELECT userid,content FROM privatechat where type = 0 and content <> '我在这儿呢' GROUP BY userid,content HAVING COUNT(*)>3 ) s1)" ;

        Db::execute($sql);
    }

    public function maleShowTime()
    {
        $config = ConfigSystem::where(['category' => 'push'])->column('title, content');
        return $this->pass($config['male_kefu']);
    }

    public function getIP()
    {
        $ip = '';
        try {
            $ip = (new Whip())->getValidIpAddress();
        } catch (\Exception $e) {
            
        }
        return $ip;
    }

    public function bannedRule()
    {
        $config = ConfigSystem::index('system');
        $ids = $config['rule'];
        $data = Db::name('page')->where(['id'=>['in',$ids]])->select();

        foreach ($data as &$value) {
            preg_match_all("/{(.*?)}/", $value['body'], $content);
            $content[1] = array_unique($content[1]);
            foreach ($content[1] as $item) {
                if($item == 'TIXIAN_TIME'){
                    $withdraw_config = Db::name('config_system')->where(['category'=>'withdrawtime'])->column('title, content');
                    $time = $withdraw_config['withdraw_hour'] . ":" . $withdraw_config['withdraw_minute'];
                    $value['body'] = str_replace('{'.$item.'}', $time, $value['body']);
                }else{
                    $value['body'] = str_replace('{'.$item.'}', getenv($item), $value['body']);
                }
            }
        }
        return $this->pass($data);
    }

    public function bennedList()
    {
        // return $this->pass(['week_count'=>0,'lists'=>[]]);
        $buildSql = Db::name('admin_logs')->where(['content'=>['like','封禁账号%']])->whereTime('time','-7 days')->order('id desc')->buildSql();
        $adminLogs = Db::table($buildSql .' a')->group('user_id')->order('id desc')->fetchSql(false)->limit(300)->select();
        $week_count = date('w')+4;
        $week_count = $week_count<7?7:$week_count;
        // $week_count = Db::name('admin_logs')
        //                 ->alias('a')
        //                 ->join('user b','b.id=a.user_id')
        //                 ->where(['content'=>['like','封禁账号%'], 'b.is_banned'=>1])
        //                 ->whereTime('time','week')
        //                 ->group('a.user_id')
        //                 ->count();
        $users = Db::name('user')->where(['id'=>['in',array_column($adminLogs, 'user_id')], 'is_banned'=>1, 'banned_desc'=>['<>', '账号异常'],'name'=>['<>', '已注销'],'delete_time'=>0])->column('banned_desc,banned_end_time,name','id');

        $lists = [];
        foreach ($adminLogs as $key => $value) {
            if (!isset($users[$value['user_id']])) continue;

            // 计算时间差
            $banTime = $users[$value['user_id']]['banned_end_time'];
            $banTimestamp = strtotime($banTime);
            $currentTimestamp = strtotime($value['time']);

            // 获取天数差
            $timeDifference = $banTimestamp - $currentTimestamp;
            $daysDifference = ceil($timeDifference / (60 * 60 * 24));


            $lists[] = [
                'name'  =>  $users[$value['user_id']]['name'],
                'day'   =>  $daysDifference,
                'reason'=>  $users[$value['user_id']]['banned_desc'],
            ];
        }

        return $this->pass(compact('lists','week_count'));
    }
}
